package com.gxa.jd.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.gxa.entity.PageResult;
import com.gxa.jd.mapper.GoodsOrderStatusMapper;
import com.gxa.jd.pojo.GoodsOrderStatus;
import com.gxa.jd.service.GoodsOrderStatusService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class GoodsOrderStatusServiceImpl implements GoodsOrderStatusService {

    @Autowired
    private GoodsOrderStatusMapper goodsOrderStatusMapper;

    /**
     * 返回全部记录
     * @return
     */
    public List<GoodsOrderStatus> findAll() {
        return goodsOrderStatusMapper.selectAll();
    }

    /**
     * 分页查询
     * @param page 页码
     * @param size 每页记录数
     * @return 分页结果
     */
    public PageResult<GoodsOrderStatus> findPage(int page, int size) {
        PageHelper.startPage(page,size);
        Page<GoodsOrderStatus> orderStatuss = (Page<GoodsOrderStatus>) goodsOrderStatusMapper.selectAll();
        return new PageResult<GoodsOrderStatus>(orderStatuss.getTotal(),orderStatuss.getResult());
    }

    /**
     * 条件查询
     * @param searchMap 查询条件
     * @return
     */
    public List<GoodsOrderStatus> findList(Map<String, Object> searchMap) {
        Example example = createExample(searchMap);
        return goodsOrderStatusMapper.selectByExample(example);
    }

    /**
     * 分页+条件查询
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    public PageResult<GoodsOrderStatus> findPage(Map<String, Object> searchMap, int page, int size) {
        PageHelper.startPage(page,size);
        Example example = createExample(searchMap);
        Page<GoodsOrderStatus> goodsOrderStatus = (Page<GoodsOrderStatus>) goodsOrderStatusMapper.selectByExample(example);
        return new PageResult<GoodsOrderStatus>(goodsOrderStatus.getTotal(),goodsOrderStatus.getResult());
    }

    /**
     * 根据Id查询
     * @param
     * @return
     */
    public GoodsOrderStatus findById(String orderId) {
        return goodsOrderStatusMapper.selectByPrimaryKey(orderId);
    }

    /**
     * 新增
     * @param
     */
    public void add(GoodsOrderStatus goodsOrderStatus) {
        goodsOrderStatusMapper.insert(goodsOrderStatus);
    }

    /**
     * 修改
     * @param
     */
    public void update(GoodsOrderStatus goodsOrderStatus) {
        goodsOrderStatusMapper.updateByPrimaryKeySelective(goodsOrderStatus);
    }

    /**
     *  删除
     * @param
     */
    public void delete(String orderId) {
        goodsOrderStatusMapper.deleteByPrimaryKey(orderId);
    }

    /**
     * 构建查询条件
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap){
        Example example=new Example(GoodsOrderStatus.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            // 商品订单id
            if(searchMap.get("orderId")!=null && !"".equals(searchMap.get("orderId"))){
                criteria.andLike("orderId","%"+searchMap.get("orderId")+"%");
            }

            // 订单状态 0待付款 1待发货 2待收货 3已完成 4已取消
            if(searchMap.get("status")!=null ){
                criteria.andEqualTo("status",searchMap.get("status"));
            }

        }
        return example;
    }

}
